package com.crm.controller;

import cn.hutool.extra.servlet.ServletUtil;
import com.crm.commons.domain.ResponseData;
import com.crm.entity.CrmActivity;
import com.crm.service.ICrmActivityService;
import com.crm.vo.ActivityDetail;
import com.crm.vo.PageBean;
import com.crm.vo.SysActivity;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author junxian, dou
 * @since 2024-11-25
 */
@RestController
@RequestMapping("/crmActivity")
@Api(tags = "活动管理")
public class CrmActivityController extends BaseController {

    @Autowired
    private ICrmActivityService activityService;

    /**
     * 创建市场活动
     *
     * @return
     */
    @ApiOperation(value = "创建市场活动")
    @PostMapping("/save")
    @PreAuthorize("hasAuthority('activity:save')")
    public ResponseData save(@RequestBody SysActivity activity) {
        activityService.saveSysActivity(activity);
        return ResponseData.success();
    }

    /**
     * 分页查询活动
     *
     * @return
     */
    @ApiOperation(value = "查询市场活动")
    @PostMapping("/list")
    @PreAuthorize("hasAuthority('activity:list')")
    public ResponseData pageList(@RequestBody PageBean pageBean) {
        PageBean<CrmActivity> list = activityService.pageList(pageBean);
        return ResponseData.success(list);
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @ApiOperation("删除活动")
    @PostMapping("/delete")
    @PreAuthorize("hasAuthority('activity:delete')")
    public ResponseData delete(@RequestBody String[] id) {
        activityService.deleteById(Arrays.asList(id));
        return ResponseData.success();
    }

    @ApiOperation("查询活动详情")
    @GetMapping("/get/{id}")
    @PreAuthorize("hasAuthority('activity:info')")
    public ResponseData getInfoById(@PathVariable String id) {
        CrmActivity byId = activityService.getById(id);
        return ResponseData.success(byId);
    }


    /**
     * 更新操作
     *
     * @param activity
     * @return
     */
    @ApiOperation("更新活动")
    @PostMapping("/update")
    @PreAuthorize("hasAuthority('activity:info')")
    public ResponseData getInfoById(@RequestBody SysActivity activity) {
        activityService.updateWithId(activity);
        return ResponseData.success();
    }


    @ApiOperation(value = "导出活动和有选择的导出", produces = "application/octet-stream")
    @PostMapping("/fileDownload")
    @PreAuthorize("hasAuthority('activity:info')")
    public void fileDownload(@RequestBody List<String> id, HttpServletResponse response) {
        activityService.fileDownload(id, response);
    }

    @ApiOperation("读取xls文件")
    @PostMapping("/importActivity")
    @PreAuthorize("hasAuthority('activity:info')")
    public ResponseData importActivity(MultipartFile file) {
        activityService.importActivity(file, getUserInfo());

        return ResponseData.success();
    }

    @GetMapping("/detailActivity/{id}")
    public ResponseData detailActivity(@PathVariable String id) {
        ActivityDetail activityDetail = activityService.getDetailActivity(id);
        return ResponseData.success(activityDetail);
    }
}
